package net.suncaper.psychological.mapper.impl;

import net.suncaper.psychological.mapper.VisitRecordMapper;
import net.suncaper.psychological.model.domain.VisitRecord;
import net.suncaper.psychological.common.utils.MyDbUtil;
import java.util.List;
import java.util.Map;

public class VisitRecordMapperImpl implements VisitRecordMapper {
    
    @Override
    public int insertVisitRecord(VisitRecord visitRecord) {
        String sql = "INSERT INTO visit_record(appointment_id, visitor_id, type, visit_time, risk_level, problem_type, conclusion, note) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        return MyDbUtil.executeUpdate(sql,
            visitRecord.getAppointment_id(),
            visitRecord.getVisitor_id(),
            visitRecord.getType(),
            visitRecord.getVisit_time(),
            visitRecord.getRisk_level(),
            visitRecord.getProblem_type(),
            visitRecord.getConclusion(),
            visitRecord.getNote()
        );
    }

    @Override
    public List<VisitRecord> selectInitialVisitRecords() {
        String sql = "SELECT vr.*, u.name as student_name FROM visit_record vr " +
                     "LEFT JOIN appointment a ON vr.appointment_id = a.id " +
                     "LEFT JOIN user u ON a.student_id = u.id " +
                     "WHERE vr.type = 'INITIAL' ORDER BY vr.visit_time DESC";
        
        System.out.println("=== 开始查询初访记录 ===");
        System.out.println("SQL: " + sql);
        
        List<VisitRecord> records = MyDbUtil.executeQuery(VisitRecord.class, sql);
        
        System.out.println("查询结果数量: " + records.size());
        for (VisitRecord r : records) {
            System.out.println("DEBUG: id=" + r.getId() + 
                             ", appointment_id=" + r.getAppointment_id() + 
                             ", student_name=" + r.getStudent_name() +
                             ", visit_time=" + r.getVisit_time() +
                             ", type=" + r.getType());
        }
        System.out.println("=== 查询初访记录完成 ===");
        
        return records;
    }

    @Override
    public VisitRecord selectVisitRecordById(Long id) {
        String sql = "SELECT * FROM visit_record WHERE id = ?";
        List<VisitRecord> list = MyDbUtil.executeQuery(VisitRecord.class, sql, id);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<VisitRecord> selectVisitRecordsByAppointmentId(Long appointmentId) {
        String sql = "SELECT * FROM visit_record WHERE appointment_id = ? ORDER BY visit_time DESC";
        return MyDbUtil.executeQuery(VisitRecord.class, sql, appointmentId);
    }

    @Override
    public int updateVisitRecordType(Long id, String type) {
        String sql = "UPDATE visit_record SET type = ? WHERE id = ?";
        return MyDbUtil.executeUpdate(sql, type, id);
    }

    @Override
    public int deleteVisitRecordById(Long id) {
        String sql = "DELETE FROM visit_record WHERE id = ?";
        return MyDbUtil.executeUpdate(sql, id);
    }

    @Override
    public List<VisitRecord> selectConsultVisitRecords() {
        String sql = "SELECT * FROM visit_record WHERE type = 'CONSULT' ORDER BY visit_time DESC";
        return MyDbUtil.executeQuery(VisitRecord.class, sql);
    }

    @Override
    public List<Map<String, Object>> selectConsultVisitRecordsWithAppointment() {
        String sql = "SELECT vr.*, a.scheduled_time, a.scheduled_place, a.duty_id, " +
                     "u.username as student_username, u.name as student_name, " +
                     "ds.user_id as consultant_id, ds.location as duty_location, " +
                     "c.name as consultant_name, c.phone as consultant_phone " +
                     "FROM visit_record vr " +
                     "LEFT JOIN appointment a ON vr.appointment_id = a.id " +
                     "LEFT JOIN user u ON a.student_id = u.id " +
                     "LEFT JOIN duty_schedule ds ON a.duty_id = ds.id " +
                     "LEFT JOIN user c ON ds.user_id = c.id " +
                     "WHERE vr.type = 'CONSULT' ORDER BY vr.visit_time DESC";
        return MyDbUtil.executeQueryMap(sql);
    }
} 